package com.ylbh.modules.returngoods.controller;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.pojo.goodsorder.dto.OrderShipmentDTO;
import com.ylbh.common.pojo.goodsorder.vo.ExpressCompanyVO;
import com.ylbh.common.pojo.returngoods.UserMailDTO;
import com.ylbh.common.pojo.returngoods.dto.ExecutionPaymentDTO;
import com.ylbh.common.pojo.returngoods.dto.OrderReturnToExamineDTO;
import com.ylbh.common.pojo.returngoods.dto.ShoppingMallOrderReturnListDTO;
import com.ylbh.common.pojo.returngoods.dto.UpdateOrderReturnDetailsDTO;
import com.ylbh.common.pojo.returngoods.vo.ExpressCompanyVo;
import com.ylbh.common.pojo.returngoods.vo.RefundAmountVO;
import com.ylbh.common.pojo.returngoods.vo.ShoppingMallOrderReturnDetailsVO;
import com.ylbh.common.utils.CommonResult;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.Ref;
import com.ylbh.common.utils.ShiroUtils;
import com.ylbh.modules.returngoods.service.ShoppingOrderReturnService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author czx
 * 退货
 */
@RestController
@RequestMapping("returnGoods")
public class ShoppingOrderReturnController {

    @Autowired
    private ShoppingOrderReturnService shoppingOrderReturnService;

    @ApiOperation("商城订单退货列表")
    @RequestMapping("queryShoppingMallOrderReturnList")
    @RequiresPermissions("returnGoods:queryShoppingMallOrderReturnList")
    public CommonResult<PageUtils> queryShoppingMallOrderReturnList(ShoppingMallOrderReturnListDTO orderReturnListDTO) throws ExecutionException, InterruptedException {
        PageUtils list = shoppingOrderReturnService.queryShoppingMallOrderReturnList(orderReturnListDTO);
        return Ref.ok(list);
    }

//    @SysLog("对退货/退款/换货订单进行审核操作")
//    @ApiOperation("对退货/退款/换货订单进行审核操作")
//    @RequestMapping("modifyReturnStatusById/{returnId}/{type}/{goodsCartId}")
//    @RequiresPermissions("returnGoods:modifyReturnStatusById")
//    public CommonResult<Void> modifyReturnStatusById(@PathVariable String returnId, @PathVariable String type, @PathVariable String goodsCartId, @RequestBody String remark) {
//        shoppingOrderReturnService.modifyReturnStatusById(returnId, type, goodsCartId, remark.substring(1,remark.length()-1));
//        return Ref.ok();
//    }

    @SysLog("对退货/换货订单进行审核操作")
    @ApiOperation("对退货/换货订单进行审核操作")
    @RequestMapping("modifyReturnStatusById")
    @RequiresPermissions("returnGoods:modifyReturnStatusById")
    public CommonResult<String> modifyReturnToExamine(@RequestBody OrderReturnToExamineDTO orderReturnToExamineDTO) {
        orderReturnToExamineDTO.setUpdateUserId(ShiroUtils.getUserId());
        String msg = shoppingOrderReturnService.modifyReturnToExamine(orderReturnToExamineDTO);
        return Ref.ok(msg);
    }

    @SysLog("对退货/换货订单进行发货操作")
    @ApiOperation("对退货/换货订单进行发货操作")
    @RequestMapping("shipmentByExchange")
//    @RequiresPermissions("returnGoods:shipmentByExchange")
    public CommonResult<String> shipmentByExchange(@RequestBody OrderReturnToExamineDTO orderReturnToExamineDTO) {
        orderReturnToExamineDTO.setUpdateUserId(ShiroUtils.getUserId());
        String msg = shoppingOrderReturnService.shipmentByExchange(orderReturnToExamineDTO);
        return Ref.ok(msg);
    }

    @ApiOperation("物流公司信息")
    @RequestMapping("queryExpress")
    public CommonResult<List<ExpressCompanyVo>> queryExpress() {
        List<ExpressCompanyVo> vo = shoppingOrderReturnService.queryExpress();
        return Ref.ok(vo);
    }

    @ApiOperation("商城订单退货列表详情")
    @RequestMapping("getShoppingMallOrderReturnDetails/{returnId}")
    @RequiresPermissions("returnGoods:getShoppingMallOrderReturnDetails")
    public CommonResult<ShoppingMallOrderReturnDetailsVO> getShoppingMallOrderReturnDetails(@PathVariable String returnId) {
        ShoppingMallOrderReturnDetailsVO vo = shoppingOrderReturnService.getShoppingMallOrderReturnDetails(returnId);
        return Ref.ok(vo);
    }

    @SysLog("确认供应商是否已经打款给公司")
    @RequestMapping("modifyMakeMoneyType/{returnId}/{type}")
    @ApiOperation("确认供应商是否已经打款给公司")
    public CommonResult<Void> modifyMakeMoneyType(@PathVariable String returnId, @PathVariable String type) {
        shoppingOrderReturnService.modifyMakeMoneyType(returnId, type);
        return Ref.ok();
    }

    @SysLog("立即执行退款")
    @RequestMapping("executionPayment")
    @ApiOperation("立即执行退款给退货用户")
    public CommonResult<String> executionPayment(@RequestBody ExecutionPaymentDTO executionPaymentDTO) {
        String msg = shoppingOrderReturnService.executionPayment(executionPaymentDTO);
        return Ref.ok(msg);
    }

    @RequestMapping("getRefundAmount/{returnId}")
    @ApiOperation("查询退款用户的退款总金额")
    public CommonResult<RefundAmountVO> getRefundAmount(@PathVariable String returnId) {
        RefundAmountVO money = shoppingOrderReturnService.getRefundAmount(returnId);
        return Ref.ok(money);
    }

    @SysLog("根据当前类型来修改物流号")
    @ApiOperation("根据当前类型来修改物流号")
    @RequestMapping("modifyLogisticsInformation/{type}")
    public CommonResult<Void> modifyLogisticsInformation(@PathVariable Integer type, @RequestBody OrderShipmentDTO orderShipmentDTO) {
        shoppingOrderReturnService.modifyLogisticsInformation(type, orderShipmentDTO);
        return Ref.ok();
    }

    @SysLog("供应商确认收货")
    @ApiOperation("供应商确认收货")
    @RequestMapping("modifyVendorConfirmation/{returnId}")
    public CommonResult<String> modifyVendorConfirmation(@PathVariable String returnId) {
        String msg = shoppingOrderReturnService.modifyVendorConfirmation(returnId);
        return Ref.ok(msg);
    }

    @SysLog("供应商确认收货(调接口）")
    @ApiOperation("供应商确认收货(调接口）")
    @RequestMapping("vendorConfirmation")
    public CommonResult<String> vendorConfirmation(@RequestBody OrderReturnToExamineDTO orderReturnToExamineDTO) {
        orderReturnToExamineDTO.setUpdateUserId(ShiroUtils.getUserId());
        String msg = shoppingOrderReturnService.vendorConfirmation(orderReturnToExamineDTO);
        return Ref.ok(msg);
    }

    @SysLog("换货订单-供应商发货")
    @ApiOperation("换货订单-供应商发货")
    @RequestMapping("modifySupplierDelivery")
    public CommonResult<Void> modifySupplierDelivery(@RequestBody OrderShipmentDTO orderShipmentDTO) {
        shoppingOrderReturnService.modifySupplierDelivery(orderShipmentDTO);
        return Ref.ok();
    }

    @SysLog("退换货核销")
    @ApiOperation("退换货核销")
    @RequestMapping("writeOffByReturnId/{returnId}")
    public CommonResult<String> writeOffByReturnId(@PathVariable String returnId) {
        String msg = shoppingOrderReturnService.writeOffByReturnId(returnId);
        return Ref.ok(msg);
    }

    @SysLog("修改退换货详情信息")
    @ApiOperation("修改退换货详情信息")
    @PostMapping("modifyReturnGoodsById")
    public CommonResult modifyReturnGoodsById(@RequestBody UpdateOrderReturnDetailsDTO dto) {
        shoppingOrderReturnService.modifyReturnGoodsById(dto);
        return Ref.ok();
    }

    @SysLog("替用户进行寄件操作")
    @ApiOperation("替用户进行寄件操作")
    @PostMapping("modifyUserMailById")
    public CommonResult modifyUserMailById(@RequestBody UserMailDTO userMailDTO) {
        if (StringUtils.isEmpty(userMailDTO.getReturnId()))
            return Ref.error("售后订单号不能为空");
        shoppingOrderReturnService.modifyUserMailById(userMailDTO);
        return Ref.ok();
    }
}
